package cn.sciento.farm.devicemanager.service.impl;

import cn.sciento.farm.devicemanager.dao.entity.DevInsectSituationLamp;
import cn.sciento.farm.devicemanager.dao.mapper.DevInsectSituationLampMapper;
import cn.sciento.farm.devicemanager.dao.mapper.DeviceMapper;
import cn.sciento.farm.devicemanager.service.DevInsectSituationLampService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 虫情灯 实现
 * @author Walton
 * @since 2020/8/18 18:36
 */
@Service
public class DevInsectSituationLampServiceImpl implements DevInsectSituationLampService {

    @Autowired
    public DevInsectSituationLampMapper insectLampMapper;

    @Autowired
    DeviceMapper deviceMapper;

    @Override
    public int insert(DevInsectSituationLamp devInsectSituationLamp) {
        if (devInsectSituationLamp.getModbusAddr() ==null) {
            devInsectSituationLamp.setModbusAddr(1);
        }
        devInsectSituationLamp.setCreateTime(System.currentTimeMillis());
        devInsectSituationLamp.setUpdateTime(System.currentTimeMillis());
        return  insectLampMapper.insert(devInsectSituationLamp);
    }

    @Override
    public List<DevInsectSituationLamp> getByEuiAndModbusAddr(String eui, Integer modbusAddr) {
        QueryWrapper queryWrapper = new QueryWrapper<DevInsectSituationLamp>();
        queryWrapper.eq("eui",eui);
        queryWrapper.eq("modbus_addr",modbusAddr);
        List<DevInsectSituationLamp> insectSituationLampList = insectLampMapper.selectList(queryWrapper);
        return insectSituationLampList;
    }

    @Override
    public int update(DevInsectSituationLamp insectLamp) {
        return insectLampMapper.updateById(insectLamp);
    }

    @Override
    public int deleteById(long id) {
        return insectLampMapper.deleteById(id);
    }

    @Override
    public  List<DevInsectSituationLamp> getByAreaId(Integer areaId) {
        QueryWrapper queryWrapper = new QueryWrapper<DevInsectSituationLamp>();
        queryWrapper.eq("area_id",areaId);
        List<DevInsectSituationLamp> insectSituationLampList = insectLampMapper.selectList(queryWrapper);
        return insectSituationLampList;
    }

    @Override
    public DevInsectSituationLamp getById(long id) {
        return insectLampMapper.selectById(id);
    }
}
